﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

// this is a comment

namespace Basic_sprite_code
{
    class Apprentice:Sprite
    {
        public MagicCircle circle;
        public StoryHelper helper;

        public int counter;
        public int animCounter;
        public int exp;
        public int evilnessLevel;
        public int maxMagic;
        public int currentMagic;
        public int spellFocus;
        public int summonFocus;

        public int maxGoblins;
        public int maxWindDemons;
        public int maxDragons;
        public int currGoblins;
        public int currWindDemons;
        public int currDragons;

        public Vector2 prevFocusLoc;

        public bool casting;


        public Apprentice(DarkLordsApprentice g, Vector2 position, StoryHelper sh)
            : base(g, "Apprentice", position, new Vector2(50, 50))
        {

            helper = sh;

            counter = 0;
            animCounter = 0;
            exp = 0;
            evilnessLevel = 1;
            maxMagic = 50;
            currentMagic = 50;
            spellFocus = 0;
            summonFocus = 0;

            maxGoblins = 8;
            maxWindDemons = 5;
            maxDragons = 3;

            currGoblins = 0;
            currWindDemons = 0;
            currDragons = 0;

            prevFocusLoc = new Vector2(0, 0);

            casting = false;
        }

        public override void Update(GameTime gameTime)
        {
            if (helper.playingGame && !game.gamePaused)
            {
                MouseState m = Mouse.GetState();
                ButtonState l = m.LeftButton;
                ButtonState r = m.RightButton;
                Vector2 currFocusLoc = new Vector2(m.X, m.Y);

                switch (exp)
                {
                    case 10:
                        evilnessLevel = 2;
                        maxMagic = 75;
                        break;
                    case 25:
                        evilnessLevel = 3;
                        maxMagic = 100;
                        break;
                    case 50:
                        evilnessLevel = 4;
                        maxMagic = 125;
                        break;
                    case 100:
                        evilnessLevel = 5;
                        maxMagic = 150;
                        break;
                    case 150:
                        evilnessLevel = 6;
                        maxMagic = 175;
                        break;
                    case 200:
                        evilnessLevel = 7;
                        maxMagic = 200;
                        break;
                    case 300:
                        evilnessLevel = 8;
                        maxMagic = 225;
                        break;
                    case 400:
                        evilnessLevel = 9;
                        maxMagic = 250;
                        break;
                    default:
                        break;
                }

                if ((l == ButtonState.Pressed || r == ButtonState.Pressed) &&
                    m.X > 100 && m.Y > 150)
                {
                    if (!casting)
                    {
                        counter = 0;
                    }
                    casting = true;
                }
                else
                {
                    casting = false;
                }

                if (casting && l == ButtonState.Pressed && m.Y < 450)
                {
                    Vector2 focusChange = currFocusLoc - prevFocusLoc;

                    if (currentMagic > 0)
                    {
                        if (focusChange.Length() == 0)
                        {
                            spellFocus++;
                            currentMagic--;
                        }
                        else if ((counter % 5 == 0))
                        {
                            game.Components.Add(new Fire(game, currFocusLoc, evilnessLevel, spellFocus));
                            spellFocus = 0;
                            currentMagic -= evilnessLevel;
                        }

                        //currentMagic--;
                    }
                }
                else if (casting && r == ButtonState.Pressed)
                {
                    if (currentMagic > 0)
                    {
                        if (m.Y < 450 && m.Y > 150)
                        {
                            summonFocus++;
                            currentMagic--;
                        }
                        else if (Math.Abs((decimal)(m.X - circle.position.X)) < 60 && Math.Abs((decimal)(m.Y - circle.position.Y)) < 30)
                        {
                            if (!circle.summonPresent)
                            {
                                circle.magicAbsorbed++;
                                currentMagic--;
                            }
                        }
                    }
                }
                else if (spellFocus > 0)
                {
                    game.Components.Add(new Fire(game, currFocusLoc, evilnessLevel, spellFocus));
                    spellFocus = 0;
                }
                else if (summonFocus > 0)
                {
                    if (summonFocus >= 200 ) //fix back
                    {
                        if (currDragons < maxDragons)
                        {
                            game.Components.Add(new Dragon(game, this, currFocusLoc, false, summonFocus - 200));
                            currDragons++;
                        }
                    }
                    else if (summonFocus >= 125)
                    {
                        if (currWindDemons < maxWindDemons)
                        {
                            game.Components.Add(new WindDemon(game, this, currFocusLoc, false, summonFocus - 125));
                            currWindDemons++;
                        }
                    }
                    else if (summonFocus >= 50)
                    {
                        if (currGoblins < maxGoblins)
                        {
                            game.Components.Add(new Goblin(game, this, currFocusLoc, false, summonFocus - 50));
                            currGoblins++;
                        }
                    }
                    summonFocus = 0;
                }
                else
                {
                    if (currentMagic < maxMagic)// && counter % 5 == 0)
                    {
                        if (evilnessLevel > 5)
                        {
                            currentMagic += 2;
                        }
                        else
                        {
                            currentMagic++;
                        }
                    }
                    if (counter == 0)
                    {
                        image = game.Content.Load<Texture2D>("Apprentice");
                    }
                }

                prevFocusLoc = currFocusLoc;
                switch (animCounter)
                {
                    case 0:
                        if (casting)
                        {
                            image = game.Content.Load<Texture2D>("ApprenticeCasting1");
                            animCounter++;
                        }
                        else
                        {
                            image = game.Content.Load<Texture2D>("Apprentice");
                        }
                        break;

                    case 20:
                        image = game.Content.Load<Texture2D>("ApprenticeCasting2");
                        animCounter++;
                        break;

                    case 40:
                        image = game.Content.Load<Texture2D>("ApprenticeCasting3");
                        animCounter++;
                        break;

                    case 60:
                        animCounter = 0;
                        break;

                    default:
                        animCounter++;
                        break;
                }

                counter++;
                base.Update(gameTime);
            }
        }

        public void setMagicCircle(MagicCircle mc) {
            circle = mc;
        }

        
    }
}
